package com.whub507.msgmanager.controller;


import com.whub507.msgmanager.common.Field;
import com.whub507.msgmanager.common.ResResult;
import com.whub507.msgmanager.common.vo.ApprovingProjectVO;
import com.whub507.msgmanager.domain.Project;
import com.whub507.msgmanager.service.ProjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.apache.el.util.Validation;
import org.apache.ibatis.annotations.Delete;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/project")
@Api(tags = "项目管理接口")
public class ProjectController {
    @Resource
    ProjectService projectService;

    @GetMapping("/all")
    @ApiOperation(value = "获取所有项目")
    public ResResult<List<Project>> getAllProjects(Integer userId){
        List<Field> fields=new ArrayList<>();
        fields.add(new Field("id","项目id"));
        fields.add(new Field("name","项目名称"));
        fields.add(new Field("detail","项目说明"));
        fields.add(new Field("createtime", "创建时间"));
        fields.add(new Field("senderid","发送者id"));
        return ResResult.ok(projectService.listAllProject(userId),fields);
    }

    @GetMapping("/getApproving")
    @ApiOperation(value = "处负责人获取待审批项目")
    public ResResult<List<ApprovingProjectVO>> getApproveProject(Integer userId){
        List<Field> fields=new ArrayList<>();
        fields.add(new Field("name","项目名称"));
        fields.add(new Field("detail","项目说明"));
        fields.add(new Field("invalidtime","审批时限"));
        fields.add(new Field("type","项目类型"));
        fields.add(new Field("sendername","申请者名字"));
        return ResResult.ok(projectService.getApproveProjects(userId), fields, "返回该用户待审批项目");
    }

    @PostMapping("/approveProjects")
    @ApiOperation(value = "通过所选项目的审批")
    public ResResult<Boolean> approveProjects(@RequestBody List<Integer> projectIds){
        String res = projectService.updateProjectsStatus(projectIds, 1);
        return (res.equals("修改成功")) ? ResResult.ok(Boolean.TRUE, res) : ResResult.error(507, res);
    }

    @PostMapping("/rejectProjects")
    @ApiOperation(value = "项目审批不通过")
    public ResResult<Boolean> rejectProjects(@RequestBody List<Integer> projectIds){
        String res = projectService.updateProjectsStatus(projectIds, 3);
        return (res.equals("修改成功")) ? ResResult.ok(Boolean.TRUE, res) : ResResult.error(507, res);
    }

    @PostMapping("/reUploadProject")
    @ApiOperation(value = "重新上传项目")
    public ResResult<Boolean> reUploadProject(Project project){
        String res = projectService.reUploadProject(project);
        return (res.equals("重新上传成功")) ? ResResult.ok(Boolean.TRUE, res) : ResResult.error(507, res);
    }

    @GetMapping("/{projectName}")
    @ApiOperation(value = "根据项目名查询项目")
    public ResResult<Project> getAllProjects(@PathVariable("projectName")String projectName){
        List<Field> fields=new ArrayList<>();
        fields.add(new Field("id","项目id"));
        fields.add(new Field("name","项目名称"));
        fields.add(new Field("detail","项目说明"));
        fields.add(new Field("createtime", "创建时间"));
        fields.add(new Field("senderid","发送者id"));
        return ResResult.ok(projectService.getProjectByName(projectName),fields);
    }


    @PostMapping("/create")
    @ApiOperation(value = "创建新项目")
    public ResResult<Boolean> createProject(Project project){
        boolean flag = projectService.insertProject(project);
        return flag?ResResult.ok(Boolean.TRUE):ResResult.error(501,"项目创建失败，请检查项目名是否重复");
    }

//    @PostMapping("/create")
//    @ApiOperation(value = "创建新项目")
//    public ResResult<Boolean> createProject(String projectName, @RequestParam(required = false) String detail, Integer senderId){
//        boolean flag = projectService.insertProject(new Project(projectName, detail, senderId));
//        return flag?ResResult.ok(Boolean.TRUE):ResResult.error(501,"项目创建失败，请检查项目名是否重复");
//    }

    @DeleteMapping("/delete")
    @ApiOperation(value="删除选择项目")
    public ResResult<Boolean> deleteProject(@RequestParam("projectNames") List<String> projectNames, Integer senderId){
//        System.out.println();
        return ResResult.ok(projectService.deleteProjects(projectNames, senderId), "删除成功");
    }

    @GetMapping("/search")
    @ApiOperation(value = "查询项目")
    public ResResult<List<Project>> searchProject(String keyWord, Integer senderId){
        List<Field> fields = new ArrayList<>();
        fields.add(new Field("id","项目id"));
        fields.add(new Field("name","项目名称"));
        fields.add(new Field("detail","项目说明"));
        fields.add(new Field("createtime", "创建时间"));
        fields.add(new Field("senderid","发送者id"));
        List<Project> projects = projectService.searchProject(keyWord, senderId);
        return ResResult.ok(projects, fields, "查询项目返回成功");
    }



}
